﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Areaexportimg.Model;

namespace Areaexportimg.Utils
{
    public class SqlHelper
    {
        public static string GetLimit(int page, int pageCount)
        {
            return $" limit {(page - 1) * pageCount}, {pageCount}";
        }
        public static Tuple<string, DynamicParameters> GetWhere(List<Wherevalue> wherevalues, string tablename = "")
        {
            if (wherevalues.Count == 0)
            {
                return new Tuple<string, DynamicParameters>(string.Empty, null);
            }
            var wheresql = new StringBuilder();
            var p = new DynamicParameters();
            wheresql.Append(" where 1=1");
            tablename = tablename.IsNullOrEmpty() ? "" : $"{tablename}.";
            foreach (var wherevalue in wherevalues)
            {

                if (!string.IsNullOrEmpty(wherevalue.Value))
                {
                    if (wherevalue.IsLike)
                    {
                        wheresql.Append($" and {tablename}{wherevalue.PropertyKey} like @{wherevalue.PropertyKey}");
                        p.Add(wherevalue.PropertyKey, wherevalue.Value + "%");
                    }
                    else
                    {
                        wheresql.Append($" and {tablename}{wherevalue.PropertyKey} = @{wherevalue.PropertyKey}");
                        p.Add(wherevalue.PropertyKey, wherevalue.Value);
                    }
                }
            }
            return new Tuple<string, DynamicParameters>(wheresql.ToString(), p);
        }
    }
}
